package com.example.controller;

import com.example.pojo.Emp;
import com.example.pojo.LoginEmp;
import com.example.pojo.Result;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping
    public Result login(@RequestBody Emp emp){
        Emp loginEmp = empService.login(emp);
        if (loginEmp == null){
            return Result.error("用户名或密码错误");
        }else {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginEmp.getId());
            claims.put("username", loginEmp.getUsername());
            String token = JwtUtils.generateJwt(claims);
            LoginEmp logEmp = new LoginEmp(loginEmp.getId(), loginEmp.getUsername(), loginEmp.getName(), token);
            return Result.success(logEmp);
        }
    }
}
